package com.fsxgt.datagrid.datax;


import static com.fsxgt.datagrid.datax.tool.DataXConstant.DEFAULT_DATAX_PY;
import static com.fsxgt.datagrid.datax.tool.DataXConstant.JVM_CM;
import static com.fsxgt.datagrid.datax.tool.DataXConstant.PARAMS_CM;
import static com.fsxgt.datagrid.datax.tool.DataXConstant.PERCENT;
import static com.fsxgt.datagrid.datax.tool.DataXConstant.SPLIT_SPACE;
import static com.fsxgt.datagrid.datax.tool.DataXConstant.TRANSFORM_QUOTES;
import static com.fsxgt.datagrid.datax.tool.DataXConstant.TRANSFORM_SPLIT_SPACE;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import lombok.Data;

/**
 * DataX command build
 *
 * @author jingwk 2020-06-07
 */
public class BuildCommand {

    /**
     * DataX command build
     * @param tgParam
     * @param tmpFilePath
     * @param dataXPyPath
     * @return
     */
    public static String[] buildDataXExecutorCmd(String dataXHomePath,TriggerParam tgParam, String tmpFilePath, String dataXPyPath) {
        // command process
        //"--loglevel=debug"
        List<String> cmdArr = new ArrayList<>();
        cmdArr.add("python");
        if (StringUtils.isNotEmpty(dataXHomePath)) {
            dataXPyPath = dataXHomePath.contains("bin") ? dataXHomePath + DEFAULT_DATAX_PY : dataXHomePath + "bin" + File.separator + DEFAULT_DATAX_PY;
        }
        cmdArr.add(dataXPyPath);
        String doc = buildDataXParam(tgParam);
        if (StringUtils.isNotBlank(doc)) {
            cmdArr.add(doc.replaceAll(SPLIT_SPACE, TRANSFORM_SPLIT_SPACE));
        }
        cmdArr.add(tmpFilePath);
        return cmdArr.toArray(new String[cmdArr.size()]);
    }
    
    @Data
    public static class TriggerParam{
    	private String jvmParam;
    	private Integer incrementType;
    	private String replaceParam;
    	private String replaceParamType;
    	private Date startTime;
    	private Date triggerTime;
    	private Long startId;
    	private Long endId;
    }

    private static String buildDataXParam(TriggerParam tgParam) {
        StringBuilder doc = new StringBuilder();
        String jvmParam = StringUtils.isNotBlank(tgParam.getJvmParam()) ? tgParam.getJvmParam().trim() : tgParam.getJvmParam();
     
        if (StringUtils.isNotBlank(jvmParam)) {
            doc.append(JVM_CM).append(TRANSFORM_QUOTES).append(jvmParam).append(TRANSFORM_QUOTES);
        }

        Integer incrementType = tgParam.getIncrementType();
        String replaceParam = StringUtils.isNotBlank(tgParam.getReplaceParam()) ? tgParam.getReplaceParam().trim() : null;

        if (incrementType != null && replaceParam != null) {

            if (DataxIncrementTypeEnum.TIME.getCode() == incrementType) {
                if (doc.length() > 0) doc.append(SPLIT_SPACE);
                String replaceParamType = tgParam.getReplaceParamType();

                if (StringUtils.isBlank(replaceParamType) || replaceParamType.equals("Timestamp")) {
                    long startTime = tgParam.getStartTime().getTime() / 1000;
                    long endTime = tgParam.getTriggerTime().getTime() / 1000;
                    doc.append(PARAMS_CM).append(TRANSFORM_QUOTES).append(String.format(replaceParam, startTime, endTime));
                } else {
                    SimpleDateFormat sdf = new SimpleDateFormat(replaceParamType);
                    String endTime = sdf.format(tgParam.getTriggerTime()).replaceAll(SPLIT_SPACE, PERCENT);
                    String startTime = sdf.format(tgParam.getStartTime()).replaceAll(SPLIT_SPACE, PERCENT);
                    doc.append(PARAMS_CM).append(TRANSFORM_QUOTES).append(String.format(replaceParam, startTime, endTime));
                }
                //buildPartitionCM(doc, partitionStr);
                doc.append(TRANSFORM_QUOTES);

            } else if (DataxIncrementTypeEnum.ID.getCode() == incrementType) {
                long startId = tgParam.getStartId();
                long endId = tgParam.getEndId();
                if (doc.length() > 0) doc.append(SPLIT_SPACE);
                doc.append(PARAMS_CM).append(TRANSFORM_QUOTES).append(String.format(replaceParam, startId, endId));
                doc.append(TRANSFORM_QUOTES);
            }
        }

        return doc.toString();
    }
    
    public static void main(String args[]) {
    	TriggerParam triggerParam=new TriggerParam();
    	triggerParam.setEndId(100000l);
    	triggerParam.setStartId(0l);
    	triggerParam.setIncrementType(1);
    	triggerParam.setJvmParam("");
    	triggerParam.setReplaceParam("-DstartId=%s -DendId=%s");
    	triggerParam.setReplaceParamType("Timestamp");
    	triggerParam.setStartTime(new Date());
    	triggerParam.setTriggerTime(new Date());
    	
    	
    	String[] ss=buildDataXExecutorCmd("dataXHomePath",triggerParam, "tmpFilePath", "dataXPyPath");
    	
    	for(String s:ss) {
    		System.out.println(s);
    	}
    	

    }




}
